from segmentation import detectTeethValley
from watershedding import getHighlightedTopTeeth, getHighlightedBottomTeeth
from classifier import hammingDistance
from boundingBox import getBoundedBox
from visualization import drawscaled
import fnmatch
import os
import cv2
from imageReader import getPanoramicImage

def segment(image):
    detectTeethValley(image)    
    for filename in fnmatch.filter(os.listdir("Intermediary images"),'top_teeth_*.tif'):
        teethImage = cv2.imread("Intermediary images/"+filename)
        teethNr = int(filename.lstrip('top_teeth_').rstrip('.tif'))
        getHighlightedTopTeeth(image,teethImage,teethNr)
        
    for filename in fnmatch.filter(os.listdir("Intermediary images"),'bottom_teeth_*.tif'):
        teethImage = cv2.imread("Intermediary images/"+filename)
        teethNr = int(filename.lstrip('bottom_teeth_').rstrip('.tif'))
        getHighlightedBottomTeeth(image,teethImage,teethNr)
        
def classify():
    for filename in fnmatch.filter(os.listdir("Watershedded images"),'top_teeth_*.png'):
        regionImage = cv2.imread("Watershedded images/"+filename)
        regionImage = cv2.cvtColor(regionImage, cv2.COLOR_BGR2GRAY)
        boundedRegionImage = getBoundedBox(regionImage)
        for referencefilename in fnmatch.filter(os.listdir("Reference boxes"),'??-[0123].png'):
            referenceImage = cv2.imread("Reference boxes/"+referencefilename)
            referenceImage = cv2.cvtColor(referenceImage, cv2.COLOR_BGR2GRAY)
            distance = hammingDistance(boundedRegionImage, referenceImage)
            print str(filename) + ": " + str(distance)
            
    for filename in fnmatch.filter(os.listdir("Watershedded images"),'bottom_teeth_*.png'):
        regionImage = cv2.imread("Watershedded images/"+filename)
        regionImage = cv2.cvtColor(regionImage, cv2.COLOR_BGR2GRAY)
        boundedRegionImage = getBoundedBox(regionImage)
        for referencefilename in fnmatch.filter(os.listdir("Reference boxes"),'??-[4567].png'):
            referenceImage = cv2.imread("Reference boxes/"+referencefilename)
            referenceImage = cv2.cvtColor(referenceImage, cv2.COLOR_BGR2GRAY)
            distance = hammingDistance(boundedRegionImage, referenceImage)
            print str(filename) + ": " + str(distance)

